package xiangmu

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type HuiyisRBody struct {
	Data       []models.Huiyi `json:"data"`
	PageNo     int            `json:"pageNo"`
	PageSize   int            `json:"pageSize"`
	TotalCount int64          `json:"totalCount"`
	TotalPage  int64          `json:"totalPage"`
}

type HuiyisBody struct {
	Page     int    `json:"pageNo" from:"pageNo"`
	Pagesize int    `json:"pageSize" from:"pageSize"`
	Zuzhiid  int64  `json:"zuzhiid" gorm:"zuzhiid"`
	Hytime   string `json:"hytime" gorm:"hytime"`
	Title    string `json:"title" gorm:"title"`
}

func Huiyis(ctx *gin.Context) {
	request := HuiyisBody{}
	ctx.ShouldBind(&request)
	zuzhiid := request.Zuzhiid
	hytime := request.Hytime
	title := request.Title
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Huiyi
	res := data_source.Db

	if zuzhiid > 0 {
		res = res.Where("zuzhiid = ?", zuzhiid)
	}
	if hytime != "" {
		res = res.Where("hytime = ?", hytime)
	}
	if title != "" {
		res = res.Where("title like ?", "%"+title+"%")
	}

	res.Model(&models.Huiyi{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var fupanidarr []int64
	for _, f := range users {
		fupanidarr = append(fupanidarr, f.ID)
	}
	var admins []models.Huiyiadmin
	data_source.Db.Where("huiyiid in (?)", fupanidarr).Find(&admins)
	for k, f := range users {
		var fupanadmins []models.Huiyiadmin
		for _, a := range admins {
			if f.ID == a.Huiyiid {
				fupanadmins = append(fupanadmins, a)
			}
		}
		users[k].Admins = fupanadmins
	}

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := HuiyisRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type HuiyiBody struct {
	ID           int64  `json:"id" gorm:"id"`
	Name         string `json:"name" gorm:"name"`
	Hytime       string `json:"hytime" gorm:"hytime"`   // 会议时间
	Adminid      int64  `json:"adminid" gorm:"adminid"` // 添加人id
	Area         string `json:"area" gorm:"area"`
	Zuzhi        string `json:"zuzhi" gorm:"zuzhi"`
	Zuzhiid      int64  `json:"zuzhiid" gorm:"zuzhiid"`
	Jiyao        string `json:"jiyao" gorm:"jiyao"`
	Jiyaoid      int64  `json:"jiyaoid" gorm:"jiyaoid"`
	Shenhe       string `json:"shenhe" gorm:"shenhe"`
	Shenheid     int64  `json:"shenheid" gorm:"shenheid"`
	Title        string `json:"title" gorm:"title"`
	Content      string `json:"content" gorm:"content"`
	Adminuserids string `json:"adminuserids" gorm:"adminuserids"`
	Jstime       string `json:"jstime" gorm:"jstime"` // 会议结束时间
}

func Huiyiadd(ctx *gin.Context) {
	request := HuiyiBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	adminid := request.Adminid
	hytime := request.Hytime
	jstime := request.Jstime
	area := request.Area
	zuzhi := request.Zuzhi
	zuzhiid := request.Zuzhiid
	jiyao := request.Jiyao
	jiyaoid := request.Jiyaoid
	shenhe := request.Shenhe
	shenheid := request.Shenheid
	title := request.Title
	content := request.Content
	adminuserids := request.Adminuserids
	var chengyuanarr []string
	chengyuanidarr := strings.Split(adminuserids, ",")
	var admins []models.Adminuser
	data_source.Db.Where("id in (?)", chengyuanidarr).Find(&admins)
	for _, a := range admins {
		chengyuanarr = append(chengyuanarr, a.Name)
	}
	huiyiadd := models.Huiyi{
		Name:     name,
		Adminid:  adminid,
		Hytime:   hytime,
		Jstime:   jstime,
		Area:     area,
		Zuzhi:    zuzhi,
		Zuzhiid:  zuzhiid,
		Jiyao:    jiyao,
		Jiyaoid:  jiyaoid,
		Shenhe:   shenhe,
		Shenheid: shenheid,
		Title:    title,
		Content:  content,
	}
	data_source.Db.Create(&huiyiadd)
	var chengyuans []models.Huiyiadmin
	var adminuserarr []string

	for _, v := range admins {
		adminuserarr = append(adminuserarr, v.Name)
		cy := models.Huiyiadmin{
			Adminuser: v.Name,
			Adminid:   v.ID,
			Huiyiid:   huiyiadd.ID,
		}
		chengyuans = append(chengyuans, cy)
	}
	data_source.Db.Create(&chengyuans)

	data_source.Db.Model(&models.Huiyi{}).Where("id = ?", huiyiadd.ID).Update("adminusers", strings.Join(adminuserarr, ","))

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    huiyiadd.ID,
		"msg":     "ok",
	})
	return

}

func Huiyiupdate(ctx *gin.Context) {
	request := HuiyiBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	jstime := request.Jstime
	name := request.Name
	hytime := request.Hytime
	area := request.Area
	zuzhi := request.Zuzhi
	zuzhiid := request.Zuzhiid
	jiyao := request.Jiyao
	jiyaoid := request.Jiyaoid
	shenhe := request.Shenhe
	shenheid := request.Shenheid
	title := request.Title
	content := request.Content
	adminuserids := request.Adminuserids
	var chengyuanarr []string
	chengyuanidarr := strings.Split(adminuserids, ",")
	var admins []models.Adminuser
	data_source.Db.Where("id in (?)", chengyuanidarr).Find(&admins)
	for _, a := range admins {
		chengyuanarr = append(chengyuanarr, a.Name)
	}
	huiyiadd := models.Huiyi{
		Name:     name,
		Hytime:   hytime,
		Area:     area,
		Zuzhi:    zuzhi,
		Zuzhiid:  zuzhiid,
		Jiyao:    jiyao,
		Jiyaoid:  jiyaoid,
		Shenhe:   shenhe,
		Shenheid: shenheid,
		Title:    title,
		Content:  content,
		Jstime:   jstime,
	}
	data_source.Db.Where("id = ?", id).Updates(&huiyiadd)
	var chengyuans []models.Huiyiadmin
	var adminuserarr []string
	data_source.Db.Where("huiyiid = ?", id).Delete(&models.Huiyiadmin{})
	for _, v := range admins {
		adminuserarr = append(adminuserarr, v.Name)
		cy := models.Huiyiadmin{
			Adminuser: v.Name,
			Adminid:   v.ID,
			Huiyiid:   id,
		}
		chengyuans = append(chengyuans, cy)
	}
	data_source.Db.Create(&chengyuans)

	data_source.Db.Model(&models.Huiyi{}).Where("id = ?", id).Update("adminusers", strings.Join(adminuserarr, ","))

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Huiyidel(ctx *gin.Context) {
	request := HuiyiBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Huiyi{})

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type HuiyiswsRBody struct {
	Data       []models.Huiyisw `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}

type HuiyiswsBody struct {
	Page     int   `json:"pageNo" from:"pageNo"`
	Pagesize int   `json:"pageSize" from:"pageSize"`
	Huiyiid  int64 `json:"huiyiid" gorm:"huiyiid"`

	Zerenid   int64  `json:"zerenid" gorm:"zerenid"`
	Gztime    string `json:"gztime" gorm:"gztime"`
	Genzongid int64  `json:"genzongid" gorm:"genzongid"`
}

func Huiyisws(ctx *gin.Context) {
	request := HuiyiswsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	huiyiid := request.Huiyiid
	zerenid := request.Zerenid
	genzongid := request.Genzongid
	gztime := request.Gztime
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Huiyisw
	res := data_source.Db
	if huiyiid > 0 {
		res = res.Where("huiyiid = ?", huiyiid)
	}
	if zerenid > 0 {
		res = res.Where("zerenid = ?", zerenid)
	}
	if genzongid > 0 {
		res = res.Where("genzongid = ?", genzongid)
	}
	if gztime != "" {
		res = res.Where("gztime = ?", gztime)
	}
	res.Model(&models.Huiyisw{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := HuiyiswsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type HuiyiswBody struct {
	ID         int64  `json:"id" gorm:"id"`
	Huiyiid    int64  `json:"huiyiid" gorm:"huiyiid"`
	Wenti      string `json:"wenti" gorm:"wenti"`
	Tichuren   string `json:"tichuren" gorm:"tichuren"`
	Tichurenid int64  `json:"tichurenid" gorm:"tichurenid"`
	Zeren      string `json:"zeren" gorm:"zeren"`
	Zerenid    int64  `json:"zerenid" gorm:"zerenid"`
	Gztime     string `json:"gztime" gorm:"gztime"`
	Genzong    string `json:"genzong" gorm:"genzong"`
	Genzongid  int64  `json:"genzongid" gorm:"genzongid"`
}

func Huiyiswadd(ctx *gin.Context) {
	request := HuiyiswBody{}
	ctx.ShouldBind(&request)
	huiyiid := request.Huiyiid
	wenti := request.Wenti
	tichuren := request.Tichuren
	tichurenid := request.Tichurenid
	zeren := request.Zeren
	zerenid := request.Zerenid
	gztime := request.Gztime
	genzong := request.Genzong
	genzongid := request.Genzongid

	huiyiadd := models.Huiyisw{
		Huiyiid:    huiyiid,
		Wenti:      wenti,
		Tichuren:   tichuren,
		Tichurenid: tichurenid,
		Zerenid:    zerenid,
		Zeren:      zeren,
		Gztime:     gztime,
		Genzong:    genzong,
		Genzongid:  genzongid,
	}
	data_source.Db.Create(&huiyiadd)

	dateStr := "2006-01-02 15:04:05"

	t, err := time.Parse(dateStr, gztime)
	if err != nil {
		fmt.Println(err)
		return
	}
	var huiyi models.Huiyi
	data_source.Db.Where("id = ?", huiyiid).First(&huiyi)
	timestamp := t.Unix()
	mb := models.Xiangmusw{
		Addtime:   time.Now().Unix(),
		Wctime:    timestamp,
		Status:    1,
		Xiangmuid: 0,
		Adminid:   0,
		Shixiang:  wenti,
		Tcr:       tichuren,
		Tcrid:     tichurenid,
		Zrr:       zeren,
		Zrrid:     zerenid,
		Gzzrr:     genzong,
		Gzzrrid:   genzongid,
		Laiyuan:   huiyi.Name,
	}
	data_source.Db.Create(&mb)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
func dateToTimestamp(dateStr string, layout string) (int64, error) {
	t, err := time.Parse(layout, dateStr)
	if err != nil {
		return 0, err
	}
	return t.Unix(), nil
}
func Huiyiswupdate(ctx *gin.Context) {
	request := HuiyiswBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	huiyiid := request.Huiyiid
	wenti := request.Wenti
	tichuren := request.Tichuren
	tichurenid := request.Tichurenid
	zeren := request.Zeren
	zerenid := request.Zerenid
	gztime := request.Gztime
	genzong := request.Genzong
	genzongid := request.Genzongid

	huiyiadd := models.Huiyisw{
		Huiyiid:    huiyiid,
		Wenti:      wenti,
		Tichuren:   tichuren,
		Tichurenid: tichurenid,
		Zerenid:    zerenid,
		Zeren:      zeren,
		Gztime:     gztime,
		Genzong:    genzong,
		Genzongid:  genzongid,
	}
	data_source.Db.Where("id = ?", id).Updates(&huiyiadd)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Huiyiswdel(ctx *gin.Context) {
	request := HuiyiswBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Huiyisw{})

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
